package com.ruoyi.mobile.system;

import com.ruoyi.db.common.domain.query.SysConfigQueryVo;
import com.ruoyi.db.common.mapper.SysConfigMapper;
import com.ruoyi.db.system.domain.SysConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SystemConfigService
{
    @Resource
    private SysConfigMapper sysConfigMapper;

    public Map<String, String> queryAll()
    {
        SysConfigQueryVo configQueryVo = new SysConfigQueryVo();
        configQueryVo.setConfigType("N");
        List<SysConfig> systemList = sysConfigMapper.selectConfigList(configQueryVo);

        Map<String, String> systemConfigs = new HashMap<>();
        for (SysConfig item : systemList)
        {
            systemConfigs.put(item.getConfigKey(), item.getConfigValue());
        }

        return systemConfigs;
    }

    public Map<String, String> listSomeAspect(String aspectName)
    {
        SysConfigQueryVo configQueryVo = new SysConfigQueryVo();
        configQueryVo.setConfigKey(aspectName);
        List<SysConfig> systemList = sysConfigMapper.selectConfigList(configQueryVo);

        Map<String, String> data = new HashMap<>();
        for (SysConfig system : systemList)
        {
            data.put(system.getConfigKey(), system.getConfigValue());
        }
        return data;
    }

    public void addConfig(String key, String value)
    {
        SysConfig config = new SysConfig();
        config.setConfigName(key);
        config.setConfigKey(key);
        config.setConfigValue(value);
        config.setConfigType("N");
        config.setCreateTime(LocalDateTime.now());
        config.setCreateBy("admin");
        config.setUpdateTime(LocalDateTime.now());
        sysConfigMapper.insertConfig(config);
    }
}
